//
//  CollectListViewController.m
//  A段项目音乐播放
//
//  Created by dllo on 15/6/2.
//  Copyright (c) 2015年 lanou3g.com蓝鸥科技. All rights reserved.
//

#import "CollectListViewController.h"
#import "CollectDatabase.h"
#import "playViewController.h"
#import "SingerModel.h"
#import "SingerState.h"
#import "CollectListCell.h"
#import "myButton.h"

@interface CollectListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,retain)NSMutableArray *array;
@property (nonatomic,retain)NSMutableArray *modelArray;
@property (nonatomic,retain)UITableView *table;

@end

@implementation CollectListViewController

- (void)dealloc
{
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
        self.modelArray = [NSMutableArray array];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"我的收藏";
    [self takeout];
    [self create];
}

- (void)takeout
{
    NSArray *array = [NSMutableArray array];
    array = [CollectDatabase selectAll];
    [self.array addObjectsFromArray:array];
}


- (void)create
{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 * 2) style:UITableViewStylePlain];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.array count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellident = @"cell";
    CollectListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellident];
    if (!cell) {
        cell = [[CollectListCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellident];
    }
    
    SingerModel *model = [self.array objectAtIndex:indexPath.row];
    cell.button.row = indexPath.row;
    [cell.button addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
    cell.label.text = model.name;
    
    return cell;
}

- (void)button:(myButton *)sender
{
    myButton *button = (myButton *)sender;
    SingerModel *model = [self.array objectAtIndex:button.row];
    SingerState *singer = [SingerState shareOfPlayState];
    singer.addNum ++;  //添加数量
    
    [singer.playListAddress insertObject:model atIndex:singer.num + singer.addNum];
    playViewController *play = [[[playViewController alloc]init] autorelease];
    [play.songListTableview reloadData];
    NSLog(@"插入%@", model.name);

}


// cell 点击协议
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    playViewController *play = [playViewController shareStateOfViewcontroller];
    [play playWithModel:self.array index:indexPath.row];
    [self.navigationController presentViewController:play animated:YES completion:nil];
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

// 设置cell 的编辑样式

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (indexPath.row == 0) {
        return UITableViewCellEditingStyleDelete;
//    }else{
//        return UITableViewCellEditingStyleInsert;
//    }
}

// 提交编辑结果
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        SingerModel *model = [self.array objectAtIndex:indexPath.row];
        [CollectDatabase deleteWithModel:model];
        
        [self.array removeObjectAtIndex:indexPath.row];
        [self.table reloadData];
    }
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.frame = CGRectMake(-320, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height);
    
    [UIView animateWithDuration:0.7 animations:^{
        cell.frame = CGRectMake(0, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height);
    } completion:^(BOOL finished) {
        ;
    }];
    

//        CATransform3D rotation;
//        rotation = CATransform3DMakeRotation( (20.0*M_PI)/180, 0.0, 0.7, 0.4);
//        rotation.m33 = 0.1;
//    
//        cell.layer.shadowColor = [[UIColor blackColor]CGColor];
//        cell.layer.shadowOffset = CGSizeMake(10, 10);
//        cell.alpha = 0;
//        cell.layer.transform = rotation;
//        cell.layer.anchorPoint = CGPointMake(0, 0.5);
//    
//    
//        [UIView beginAnimations:@"rotation" context:NULL];
//        [UIView setAnimationDuration:1];
//        cell.layer.transform = CATransform3DIdentity;
//        cell.alpha = 1;
//        cell.layer.shadowOffset = CGSizeMake(0, 0);
//        [UIView commitAnimations];

}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
